package model

import (
	"context"
	"gorm.io/gorm"
	"time"
	"video_08_05/global"
)

type Comment struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:评论ID;primaryKey;not null;" form:"id" json:"id"`       // 评论ID
	UserId    int32          `gorm:"column:user_id;type:int;comment:用户ID;not null;" form:"user_id" json:"user_id"`            // 用户ID
	VideoId   int32          `gorm:"column:video_id;type:int;comment:视频ID;not null;" form:"video_id" json:"video_id"`         // 视频ID
	Content   string         `gorm:"column:content;type:varchar(300);comment:评论内容;not null;" form:"content" json:"content"` // 评论内容
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" form:"created_at" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" form:"updated_at" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" form:"deleted_at" json:"deleted_at"`
}

func (c *Comment) TableName() string {
	return "comment"
}

func (c *Comment) AddComment() error {
	return global.DB.Create(&c).Error
}

func (c *Comment) DeletedByUserIdComment() error {
	return global.DB.Where("user_id = ?", c.UserId).Delete(&c).Error
}

func (s *SubComment) ListByAuthIdSubComment() (sub []SubComment, err error) {
	err = global.DB.Where("user_id = ?", s.UserId).Find(&sub).Error
	return
}

type SubComment struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:子评论ID;primaryKey;not null;" form:"id" json:"id"`     // 子评论ID
	ParentId  int32          `gorm:"column:parent_id;type:int;comment:父评论ID;not null;" form:"parent_id" json:"parent_id"`    // 父评论ID
	RootId    int32          `gorm:"column:root_id;type:int;comment:根评论ID;not null;" form:"root_id" json:"root_id"`          // 根评论ID
	UserId    int32          `gorm:"column:user_id;type:int;comment:用户ID;not null;" form:"user_id" json:"user_id"`            // 用户ID
	VideoId   int32          `gorm:"column:video_id;type:int;comment:视频ID;not null;" form:"video_id" json:"video_id"`         // 视频ID
	Content   string         `gorm:"column:content;type:varchar(300);comment:评论内容;not null;" form:"content" json:"content"` // 评论内容
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" form:"created_at" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" form:"updated_at" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" form:"deleted_at" json:"deleted_at"`
}

func (s *SubComment) TableName() string {
	return "sub_comment"
}

func (s *SubComment) AddSubComment() error {
	return global.DB.Create(&s).Error
}

func (s *SubComment) ListByRootIdSubComment() (sub []SubComment, err error) {
	err = global.DB.Where("root_id = ?", s.RootId).Find(&sub).Error
	global.RDB.Set(context.Background(), "HatComment", sub, time.Hour*24)
	return
}
